package com.android.wnba.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import com.android.wnba.R;

public class Util {
	/*********************************** 以下为UTF8处理方法 ***********************************/
	public static String getUTF8String(byte[] b) {
		if (b == null)
			return "";
		return getUTF8String(b, 0, b.length);
	}

	/**
	 * <p>
	 * Get UTF8 bytes from a string
	 * </p>
	 * 
	 * @param string
	 *            String
	 * @return UTF8 byte array, or null if failed to get UTF8 byte array
	 */
	public static byte[] getUTF8Bytes(String string) {
		if (string == null)
			return new byte[0];

		try {
			return string.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			try {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				DataOutputStream dos = new DataOutputStream(bos);
				dos.writeUTF(string);
				byte[] jdata = bos.toByteArray();
				bos.close();
				dos.close();
				byte[] buff = new byte[jdata.length - 2];
				System.arraycopy(jdata, 2, buff, 0, buff.length);
				return buff;
			} catch (Exception ex) {
				return new byte[0];
			}
		}
	}

	private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	public static String encode(byte[] binaryData) {
		if (binaryData.length != 16) {
			return null;
		}

		char[] buffer = new char[32];
		for (int i = 0; i < 16; i++) {
			int low = (binaryData[i] & 0x0f);
			int high = ((binaryData[i] & 0xf0) >> 4);
			buffer[i * 2] = HEX_DIGITS[high];
			buffer[(i * 2) + 1] = HEX_DIGITS[low];
		}

		return new String(buffer);
	}

	public static String getUTF8String(byte[] b, int start, int length) {
		if (b == null) {
			return "";
		} else {
			try {
				return new String(b, start, length, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				return "";
			}
		}
	}

	public static String Utf8URLencode(String text) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			if ((c >= 0) && (c <= 255)) {
				result.append(c);
			} else {
				byte[] b = new byte[0];
				try {
					b = Character.toString(c).getBytes("UTF-8");
				} catch (Exception ex) {
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					result.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return result.toString();
	}

	/*********************************** 以上为UTF8处理方法 ***********************************/

	/*********************************** 以下为打日志方法 ***********************************/

	/**
	 * 简单打日志
	 * 
	 * @param msg
	 */
	public static void debug(String msg) {
		try {
			if (Constants.DEBUG) {
				Log.d(Constants.TAG, "->" + msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据tag打标准日志
	 * 
	 * @param tag
	 * @param info
	 */
	public static void debugMsg(String tag, String info) {
		if (Constants.DEBUG) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";
			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}
			Log.i(tag, className + "::" + info);
		}
	}

	/**
	 * 默认tag打标准日志
	 * 
	 * @param info
	 */
	public static void debugMsg(String info) {
		if (Constants.DEBUG) {
			StackTraceElement[] stack = (new Throwable()).getStackTrace();
			StackTraceElement ele = null;
			String className = "";
			if (stack.length > 1) {
				ele = stack[1];
				try {
					className = Class.forName(ele.getClassName())
							.getSimpleName();
				} catch (ClassNotFoundException e) {
				}
			}
			Log.i(Constants.TAG, className + "::" + String.valueOf(info));
		}
	}

	/*********************************** 以上为打日志方法 ***********************************/

	/*********************************** 以下为View动画效果处理方法 ***********************************/
	public static boolean isShowingView(View view) {
		return view.getVisibility() == View.VISIBLE;
	}

	public static void hideViewDownUp(Context ctx, View view) {
		view.setVisibility(View.GONE);
		view.startAnimation(AnimationUtils.loadAnimation(ctx,
				R.anim.slide_down_up));
	}

	public static void hideViewUpDown(Context ctx, View view) {
		view.setVisibility(View.GONE);
		view.startAnimation(AnimationUtils.loadAnimation(ctx,
				R.anim.menu_up_down));
	}

	public static void showViewUpDown(Context ctx, View view) {
		view.setVisibility(View.VISIBLE);
		view.startAnimation(AnimationUtils.loadAnimation(ctx,
				R.anim.slide_up_down));
	}

	public static void showViewDownUp(Context ctx, View view) {
		view.setVisibility(View.VISIBLE);
		view.startAnimation(AnimationUtils.loadAnimation(ctx,
				R.anim.menu_down_up));
	}

	/*********************************** 以上为View动画效果处理方法 ***********************************/

	/**
	 * 判断是否有网络
	 * 
	 * @param ctx
	 * @return
	 */
	public static boolean isNetWorking(Context ctx) {
		ConnectivityManager connectivity = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 获取IMEI
	 * 
	 * @param ctx
	 * @return
	 */
	public static String getIMEI(Context ctx) {
		// need <uses-permission
		// android:name="android.permission.READ_PHONE_STATE"/>
		TelephonyManager tm = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}

	public static void shortToast(Context context, int resId) {
		Toast.makeText(context, context.getString(resId), Toast.LENGTH_SHORT)
				.show();
	}

	public static void shortToast(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 离开程序
	 * 
	 * @param context
	 */
	public static void exitApp(Context context) {
		// PrefUtil.removeUserToken(context);
		// Intent intent = new Intent(Constants.BROADCAST_FINDER_EXIT);
		// context.sendBroadcast(intent);
	}

	/**
	 * 注销
	 * 
	 * @param context
	 */
	public static void logout(Context context) {
		// PrefUtil.removeUserToken(context);
		// Intent intent = new Intent(context, LoginActivity.class);
		// context.startActivity(intent);
	}

	public static String R(Context ctx, int i, Object... args) {
		String format = R(ctx, i);
		String rs = String.format(format, args);
		return rs;
	}

	/**
	 * 获取res字符串资源
	 * 
	 * @param ctx
	 * @param str
	 * @return
	 */
	public static String R(Context ctx, int i) {
		String rs = "";
		rs = ctx.getResources().getString(i);
		return rs;
	}

	public static String getFormatDate(Context ctx, String dateStr) {
		// if (TextUtils.isEmpty(dateStr)) {
		// return "";
		// }
		//
		// SimpleDateFormat tempformat = new SimpleDateFormat(
		// "yyyy-MM-dd HH:mm:ss");
		// Date tempDate;
		// String result = "";
		// try {
		// tempDate = tempformat.parse(dateStr);
		// result = Util.R(ctx, R.string.pub_date, tempDate.getYear() + 1900,
		// tempDate.getMonth() + 1, tempDate.getDate(),
		// tempDate.getHours(), tempDate.getMinutes());
		// } catch (ParseException e) {
		// e.printStackTrace();
		// }
		// return result;
		return null;
	}

}
